package cn.com.pc.content.util;

import org.apache.lucene.util.ArrayUtil;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class IpUtils {

    public static String getIp(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String forwarded = request.getHeader("X-Forwarded-For");
        String realIp = request.getHeader("X-Real-IP");
        String ip;
        String[] forwardedIps;
        if (realIp == null) {
            if (forwarded == null) {
                ip = remoteAddr;
            } else {
                forwardedIps = forwarded.split(",");
                if (forwardedIps.length >= 2) {
                    ip = forwardedIps[0].trim() + "/" + forwardedIps[1].trim();
                } else {
                    ip = forwarded;
                }
            }
        } else if (realIp.equals(forwarded)) {
            ip = realIp;
        } else {
            if (forwarded == null) {
                ip = remoteAddr;
            } else {
                forwardedIps = forwarded.split(",");
                if (forwardedIps.length > 2) {
                    ip = forwardedIps[0].trim() + "/" + forwardedIps[1].trim();
                } else if (forwardedIps.length == 2) {
                    ip = forwardedIps[0].trim();
                } else {
                    ip = forwarded;
                }
            }
        }

        return ip.trim();
    }

    /**
	public static String getIp(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String forwarded = request.getHeader("X-Forwarded-For");
        String realIp = request.getHeader("X-Real-IP");

        String ip = null;
        if (realIp == null) {
            if (forwarded == null) {
                ip = remoteAddr;
            } else {
                ip = remoteAddr + "/" + forwarded;
            }
        } else {
            if (realIp.equals(forwarded)) {
                ip = realIp;
            } else {
                ip = realIp + "/" + forwarded.replaceAll(", " + realIp, "");
            }
        }
        return ip;
    }
     **/

	private static String localIp;
	static {
		localIp = getLocalAddress();
	}
	public static String getIp() {
		return localIp;
	}

    @SuppressWarnings("unchecked")
	static String getLocalAddress() {
        String result = null;
        Enumeration enu = null;
        try {
            enu = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            return "localhost";
        }
        while (enu.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) (enu.nextElement());
            Enumeration ias = ni.getInetAddresses();
            while (ias.hasMoreElements()) {
                InetAddress i = (InetAddress) (ias.nextElement());
                String addr = i.getHostAddress();
                return addr;
            }
        }

        if (result == null) {
            try {
                result = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    //获取出口ip
   // Socket client = new Socket("192.168.6.8", 80);
   // System.out.println(client.getInetAddress().getHostAddress());
}
